//
//  BookHotelViewController.m
//  QYer
//
//  Created by lanou on 16/8/6.
//  Copyright © 2016年 CaiZhuYu. All rights reserved.
//

#import "BookHotelViewController.h"
#import "HeaderTopicView.h"
#import "BookListTableViewCell.h"
#import "FooterTopicView.h"

#import "HeaderTopicModel.h"

@interface BookHotelViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
//实惠
@property (nonatomic, strong) NSMutableArray *affordableArray;
//机票
@property (nonatomic, strong) NSMutableArray *ticketArray;

@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation BookHotelViewController


- (void)requestData {
    [NetWorkTool requstWithType:GET URLString:discount parameter:nil callBack:^(NSData *data, NSError *error) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        //jsonDic[@"data"][@"hot_area"]---->东南亚,欧美,港澳台
        /*
         NSArray *areaArray = jsonDic[@"data"][@"hot_area"];
         for (int i = 0; i < areaArray.count; i++) {
         NSDictionary *dictionary = areaArray[i];
         for (NSDictionary *dic in dictionary[@"place"]) {
         //
         }
         for (NSDictionary *dic in dictionary[@"list"]) {
         //
         }
         }
         */
        //jsonDic[@"data"][@"discount_topic"]---->不愁去哪玩,超值机票
        
        NSArray *discountArray = jsonDic[@"data"][@"discount_topic"];
        for (int i = 0; i < discountArray.count; i++) {
            
            if (i == 0) {
                NSDictionary *dictionary = discountArray[i];
                //topic
                HeaderTopicModel *topicModel = [[HeaderTopicModel alloc] init];
                [topicModel setValuesForKeysWithDictionary:dictionary[@"topic"]];
                [self.dataArray addObject:topicModel];
                //list
                for (NSDictionary *dic in dictionary[@"list"]) {
                    BookListModel *model = [[BookListModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.affordableArray addObject:model];
                }
                
            } else if (i == 1) {
                NSDictionary *dictionary = discountArray[i];
                //topic
                HeaderTopicModel *topicModel = [[HeaderTopicModel alloc] init];
                [topicModel setValuesForKeysWithDictionary:dictionary[@"topic"]];
                [self.dataArray addObject:topicModel];
                //list
                for (NSDictionary *dic in dictionary[@"list"]) {
                    BookListModel *model = [[BookListModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.ticketArray addObject:model];
                }
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }];
}

- (void)createSubViews {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"BookListTableViewCell" bundle:nil] forCellReuseIdentifier:@"reuse"];
}




#pragma mark -----

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.affordableArray = @[].mutableCopy;
    self.ticketArray = @[].mutableCopy;
    self.dataArray = @[].mutableCopy;
    
    
    [self requestData];
    [self createSubViews];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


#pragma mark ----- UITableViewDataSource -----

//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
//分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.affordableArray.count;
    } else {
        return self.ticketArray.count;
    }
}
//cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BookListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        BookListModel *model = self.affordableArray[indexPath.row];
        [cell setContentWithModel:model];
    } else {
        BookListModel *model = self.ticketArray[indexPath.row];
        [cell setContentWithModel:model];
    }
    return cell;
}
//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}
//头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 200;
}
//尾视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    HeaderTopicView *view = [HeaderTopicView headerTopicView];
    view.frame = CGRectMake(0, 0, kScreenWidth, 200);
    HeaderTopicModel *model = self.dataArray[section];
    [view.picView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"占位图"]];
    view.titleLabel.text = model.title;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    view.picView.userInteractionEnabled = YES;
    [view.picView addGestureRecognizer:tap];
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    FooterTopicView *view = [FooterTopicView footerTopicView];
    view.frame = CGRectMake(0, 0, kScreenWidth, 50);
    view.checkAllBtn.tag = 1000+section;
    [view.checkAllBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    return view;
}


- (void)clickAction:(UIButton *)button {
    NSLog(@"点击Button %ld", button.tag);
    if (button.tag == 1000) {
        DetailViewController *detailVC = [[DetailViewController alloc] init];
        detailVC.urlString = @"http://m.qyer.com/z/zt/sale&source=app&campaign=appshouye&category=ht1-sale&source=app";
        [self.navigationController pushViewController:detailVC animated:YES];
    } else {
        DetailViewController *detailVC = [[DetailViewController alloc] init];
        detailVC.urlString = @"http://m.qyer.com/z/zt/cqdyp&source=app&campaign=appshouye&category=ht3-cqdyp&source=app";
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

- (void)tapAction:(UIGestureRecognizer *)tap {
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    if (indexPath.section == 0) {
        BookListModel *model = self.affordableArray[indexPath.row];
        detailVC.urlString = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=7.0&track_deviceid=2D9E8D6C-2140-49DC-993D-D66133CC22C0&track_user_id=8252405", model.ID];
    } else {
        BookListModel *model = self.ticketArray[indexPath.row];
        detailVC.urlString = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=7.0&track_deviceid=2D9E8D6C-2140-49DC-993D-D66133CC22C0&track_user_id=8252405", model.ID];
    }
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
